package com.anji.tug;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.anji.tug.Objectives.ObjectiveDescriptor;

public class ObjectivesLoader {
	/**
	 * Retrieves an Objectives descriptor from an Xml with the given file name.
	 * @param resource The name of the Xml file.
	 * @return The Objectives descriptor.
	 * @throws JAXBException
	 */
	public static List<ObjectiveDescriptor> getObjectives(String resource) throws JAXBException {
		InputStream inputStream = ClassLoader.getSystemResourceAsStream( resource );
	    String packageName = Objectives.class.getPackage().getName();
	    JAXBContext jc = JAXBContext.newInstance( packageName );
	    Unmarshaller u = jc.createUnmarshaller();
	    Objectives objs = (Objectives) u.unmarshal(inputStream);
	    return objs.getObjectiveDescriptor();
	}
	
	public static void main(String[] args) throws JAXBException {
		List<ObjectiveDescriptor> ob = ObjectivesLoader.getObjectives("properties/tugObjectivesBase.xml");
	}
	
	
	public static HashMap<String, Objective> descriptorListToObjectiveHash(List<ObjectiveDescriptor> list) {
		HashMap<String, Objective> objectives = new HashMap<String, Objective>(list.size());
		for (ObjectiveDescriptor od : list) {
			objectives.put(od.name, new Objective(od.finalGoal, od.recencyAlpha, od.goalUpdateEta, od.name));
		}
		return objectives;
	}
}
